package com.zuo.mall.service.impl;

import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.dto.OmsOrderReturnApplyResult;
import com.zuo.mall.dto.OmsReturnApplyQueryParam;
import com.zuo.mall.dto.OmsUpdateStatusParam;
import com.zuo.mall.entity.OmsOrderReturnApply;
import com.zuo.mall.mapper.OmsOrderReturnApplyMapper;
import com.zuo.mall.service.OmsOrderReturnApplyService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OmsOrderReturnApplyServiceImpl
        extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply>
        implements OmsOrderReturnApplyService {

    @Override
    public CommonPage<OmsOrderReturnApply> list(OmsReturnApplyQueryParam omsReturnApplyQueryParam, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        // 服务单号
        Long id = omsReturnApplyQueryParam.getId();
        if (id!=null) {
            queryWrapper.eq(OmsOrderReturnApply::getId, id);
        }
        // 收货人姓名号码
        String keyword = omsReturnApplyQueryParam.getReceiverKeyword();
        if (!StrUtil.isEmpty(keyword)) {
            queryWrapper.like(OmsOrderReturnApply::getReturnName, keyword)
                    .or().like(OmsOrderReturnApply::getReturnPhone, keyword);
        }
        // 申请状态
        Integer status = omsReturnApplyQueryParam.getStatus();
        if (status != null) {
            queryWrapper.eq(OmsOrderReturnApply::getStatus, status);
        }
        // 申请时间
        String createTime = omsReturnApplyQueryParam.getCreateTime();
        if (!StrUtil.isEmpty(createTime)) {
            queryWrapper.like(OmsOrderReturnApply::getCreateTime, createTime);
        }
        // 处理人员
        String handleMan = omsReturnApplyQueryParam.getHandleMan();
        if (!StrUtil.isEmpty(handleMan)) {
            queryWrapper.eq(OmsOrderReturnApply::getHandleMan, handleMan);
        }
        // 处理时间
        String handleTime = omsReturnApplyQueryParam.getHandleTime();

        if (!StrUtil.isEmpty(handleTime)) {
            queryWrapper.like(OmsOrderReturnApply::getHandleTime, handleTime);
        }

        Integer total = baseMapper.selectCount(queryWrapper);
        queryWrapper.last("LIMIT "+ PageUtil.getStart(pageNum -1, pageSize) + "," + pageSize);

        List<OmsOrderReturnApply> omsOrderReturnApplies = baseMapper.selectList(queryWrapper);
        return CommonPage.restPage(omsOrderReturnApplies, Long.valueOf(total), pageSize);
    }

    @Override
    public OmsOrderReturnApplyResult getOrderReturnApplyResultById(Long id) {
        return baseMapper.getOrderReturnApplyResultById(id);
    }

    @Override
    public int updateStatus(Long id, OmsUpdateStatusParam omsUpdateStatusParam) {
        Integer status = omsUpdateStatusParam.getStatus();
        OmsOrderReturnApply returnApply = new OmsOrderReturnApply();
        // 设置
        returnApply.setId(id);


        if (status.equals(1)) {
            // 退货中
            returnApply.setStatus(1);
            returnApply.setReturnAmount(omsUpdateStatusParam.getReturnAmount());
            returnApply.setCompanyAddressId(omsUpdateStatusParam.getCompanyAddressId());
            returnApply.setHandleNote(omsUpdateStatusParam.getHandleNote());
            returnApply.setHandleTime(LocalDateTime.now());
            returnApply.setHandleMan(omsUpdateStatusParam.getHandleMan());

        }else if (status.equals(2)) {
            returnApply.setStatus(2);
            returnApply.setReceiveTime(LocalDateTime.now());
            returnApply.setReceiveMan(omsUpdateStatusParam.getReceiveMan());
            returnApply.setReceiveNote(omsUpdateStatusParam.getReceiveNote());
            // 已完成
        }else if (status.equals(3)) {
            returnApply.setHandleTime(LocalDateTime.now());
            returnApply.setStatus(3);
            returnApply.setHandleNote(omsUpdateStatusParam.getHandleNote());
            returnApply.setHandleMan(omsUpdateStatusParam.getHandleMan());
            // 拒绝
        }else {
            return 0;
        }
        int i = baseMapper.updateById(returnApply);
        return i;
    }
}
